package com.example.hello_ma.testlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class MainActivity extends AppCompatActivity {
    private List<String> list = new ArrayList<String>(15);
    private Set<SwipeListLayout> sets = new HashSet();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView lv_main = (ListView) findViewById(R.id.listview);
            initList();
            lv_main.setAdapter(new ListAdapter());
            lv_main.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    switch (scrollState) {
                        //当listview开始滑动时，若有item的状态为Open，则Close，然后移除
                        case SCROLL_STATE_TOUCH_SCROLL:
                            if (sets.size() > 0) {
                                for (SwipeListLayout s : sets) {
                                    s.setStatus(SwipeListLayout.Status.Close, true);
                                    sets.remove(s);
                                }
                            }
                            break;

                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount) {

                }
            });
        }

        private void initList(){
            list.add("A");
            list.add("B");
            list.add("C");
            list.add("D");
            list.add("E");
            list.add("F");
            list.add("G");
            list.add("H");
            list.add("I");
            list.add("J");
            list.add("K");
            list.add("L");
            list.add("M");
            list.add("N");
        }

        class MyOnSlipStatusListener implements SwipeListLayout.OnSwipeStatusListener {

            private SwipeListLayout slipListLayout;

            public MyOnSlipStatusListener(SwipeListLayout slipListLayout) {
                this.slipListLayout = slipListLayout;
            }

            @Override
            public void onStatusChanged(SwipeListLayout.Status status) {
                if (status == SwipeListLayout.Status.Open) {
                    //若有其他的item的状态为Open，则Close，然后移除
                    if (sets.size() > 0) {
                        for (SwipeListLayout s : sets) {
                            s.setStatus(SwipeListLayout.Status.Close, true);
                            sets.remove(s);
                        }
                    }
                    sets.add(slipListLayout);
                } else {
                    if (sets.contains(slipListLayout))
                        sets.remove(slipListLayout);
                }
            }

            @Override
            public void onStartCloseAnimation() {

            }

            @Override
            public void onStartOpenAnimation() {

            }

        }

        class ListAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int arg0) {
                return list.get(arg0);
            }

            @Override
            public long getItemId(int arg0) {
                return arg0;
            }

            @Override
            public View getView(final int arg0, View view, ViewGroup arg2) {
                if (view == null) {
                    view = LayoutInflater.from(MainActivity.this).inflate(
                            R.layout.layout_item, null);
                }
                TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
                tv_name.setText(list.get(arg0));
                final SwipeListLayout sll_main = (SwipeListLayout) view
                        .findViewById(R.id.sll_main);
                TextView tv_top = (TextView) view.findViewById(R.id.tv_top);
                TextView tv_delete = (TextView) view.findViewById(R.id.tv_delete);
                sll_main.setOnSwipeStatusListener(new MyOnSlipStatusListener(
                        sll_main));
                tv_top.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        sll_main.setStatus(SwipeListLayout.Status.Close, true);
                        String str = list.get(arg0);
                        list.remove(arg0);
                        list.add(0, str);
                        notifyDataSetChanged();
                    }
                });
                tv_delete.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        sll_main.setStatus(SwipeListLayout.Status.Close, true);
                        list.remove(arg0);
                        notifyDataSetChanged();
                    }
                });
                return view;
            }

        }

    }
